package com.trm.student.dao;

import java.util.List;
import java.util.Map;

import com.common.util.jdbc.BaseDao;

public class StudentDao extends BaseDao {

    /**
     * 默认页
     */
    private int page = 1;
    /**
     * 默认页大小
     */
    private int pageSize = 10;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 默认页数量
     */
    private int pageNumber;

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    /**
     * 获取用户信息列表
     * 
     * @return
     */
    public List<Map<String, Object>> getStudentList(String role) {
        // String sql = "select * from student where role = " + role + " limit " +
        // pageSize * (page - 1) + "," + pageSize;
        StringBuffer sql = new StringBuffer();
        sql.append("select * from student");
        if (role != null && !"".equals(role)) {
            sql.append(" where role = " + role);
        }
        sql.append(" limit " + pageSize * (page - 1) + "," + pageSize);
        List<Map<String, Object>> list = this.executeSqlToList(sql.toString());
        return list;
    }

    /**
     * 获取用户信息列表
     * 
     * @return
     */
    public List<Map<String, Object>> getStudentList(String role, String column, Integer page, Integer pageSize) {
        // String sql = "select * from student where role = " + role + " limit " +
        // pageSize * (page - 1) + "," + pageSize;
        StringBuffer sql = new StringBuffer();
        column += ",id,role";
        sql.append("select " + column + " from student");
        if (role != null && !"".equals(role)) {
            sql.append(" where role = " + role);
        }
        sql.append(" order by createtime desc");
        sql.append(" limit " + pageSize * (page - 1) + "," + pageSize);
        List<Map<String, Object>> list = this.executeSqlToList(sql.toString());
        return list;
    }

    /**
     * 根据角色获得用户数量
     * 
     * @param roleId
     * @return
     */
    public int getUserCountByRoleId(String roleId) {
        String sql = "select count(*) as count from student";
        if (roleId != null && !"".equals(roleId)) {
            sql += " where role = " + roleId;
        }
        Map<String, Object> countMap = this.executeSqlToMap(sql);
        int count = Integer.valueOf(countMap.get("count").toString());
        return count;
    }

    /**
     * 添加学生信息
     * 
     * @param list
     */
    public int addStudent(List<Map<String, String>> list) {
        Map<String, String> map = list.get(0);
        /*
         * String province = map.get("province"); String city = map.get("city"); String
         * county = map.get("county"); String school = map.get("school"); String college
         * = map.get("college"); String professional = map.get("professional"); String
         * course = map.get("course"); String name = map.get("name"); String sex =
         * map.get("sex"); String age = map.get("age"); String phone = map.get("phone");
         * String mail = map.get("mail"); String note = map.get("note");
         */
        String column = "";
        String values = "";
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue() != null && !"".equals(entry.getValue()) && !"null".equals(entry.getValue())) {
                column += "," + entry.getKey();
                values += ",'" + entry.getValue() + "'";
            }
        }
        if ("".equals(column) || "".equals(values)) {
            System.out.println("检查参数");
            return -1;
        }
        column = column.substring(1) + ",createtime";
        values = values.substring(1) + ",now()";
        String sql = "insert into student (" + column + ") values (" + values + ");";
        int result = this.executeSql(sql);
        return result;
    }

    /**
     * 根据id查询用户信息
     * 
     * @param id
     */
    public Map<String, Object> getUserById(String id) {
        String sql = "select * from student where id = " + id;
        Map<String, Object> map = this.executeSqlToMap(sql);
        return map;
    }

    /**
     * 根据id修改用户信息
     * 
     * @param id
     * @return
     */
    public int updateUserById(Map<String, String> params) {
        String id = params.get("id");
        if (id == null || "".equals(id)) {
            System.out.println("未获取到id");
            return -1;
        }
        String column = "";
        for (Map.Entry<String, String> entry : params.entrySet()) {
            column += "," + entry.getKey() + " = '" + entry.getValue() + "'";
        }
        if ("".equals(column)) {
            System.out.println("请检查参数");
            return -1;
        }
        column = column.substring(1) + ",updatetime = now()";
        String sql = "update student set " + column + " where id = " + id;
        int result = this.executeSql(sql);
        return result;
    }

    /**
     * 根据id删除用户
     * 
     * @param id
     * @return
     */
    public int deleteUserById(String id) {
        String sql = "delete from student where id = " + id;
        int result = this.executeSql(sql);
        return result;
    }

    /**
     * 获得角色属性
     * 
     * @param roleId
     * @return
     */
    public String getPropertiesByRole(String roleId) {
        String sql = "select property from role where id = " + roleId;
        Map<String, Object> propertyMap = this.executeSqlToMap(sql);
        return propertyMap.get("property").toString();
    }

    /**
     * 获得所有属性
     * 
     * @param properties
     * @return
     */
    public List<Map<String, Object>> getProperties(String properties) {
        String sql = "select * from property where property in (" + properties + ")";
        List<Map<String, Object>> list = this.executeSqlToList(sql);
        return list;
    }
}
